package com.dd.model;

import java.util.Objects;

public class ColumnInfo {
    String name;
    String dataType;
    String columnType;
    long size;
    long decimalDigits;
    boolean nullable;
    String defaultValue;
    boolean autoIncrement;
    String comment;

    public void setName(String name) {
        this.name = name;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public void setDecimalDigits(long decimalDigits) {
        this.decimalDigits = decimalDigits;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public void setAutoIncrement(boolean autoIncrement) {
        this.autoIncrement = autoIncrement;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getName() {
        return name;
    }

    public String getDataType() {
        return dataType;
    }

    public String getColumnType() {
        return columnType;
    }

    public long getSize() {
        return size;
    }

    public long getDecimalDigits() {
        return decimalDigits;
    }

    public boolean isNullable() {
        return nullable;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public boolean isAutoIncrement() {
        return autoIncrement;
    }

    public String getComment() {
        return comment;
    }

    public ColumnInfo(String name, String dataType, String columnType, long size, long decimalDigits, boolean nullable, String defaultValue, boolean autoIncrement, String comment) {
        this.name = name;
        this.dataType = dataType;
        this.columnType = columnType;
        this.size = size;
        this.decimalDigits = decimalDigits;
        this.nullable = nullable;
        this.defaultValue = defaultValue;
        this.autoIncrement = autoIncrement;
        this.comment = comment;
    }

    public String getDefinition() {
        StringBuilder def = new StringBuilder();

        // 使用完整的列类型定义（包括长度、精度等）
        def.append(columnType);

        def.append(nullable ? " NULL" : " NOT NULL");

        // 处理默认值
        if (autoIncrement) {
            def.append(" AUTO_INCREMENT");
        } else if (defaultValue != null) {
            // 特殊处理 CURRENT_TIMESTAMP
            if ("CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue) || defaultValue.startsWith("CURRENT_TIMESTAMP")) {
                def.append(" DEFAULT ")
                        .append(defaultValue);
            } else {
                // 为字符串类型添加引号
                if (dataType.contains("char") || dataType.contains("text") || dataType.contains("date") || dataType.contains("time")) {
                    def.append(" DEFAULT '")
                            .append(escapeSqlString(defaultValue))
                            .append("'");
                } else {
                    def.append(" DEFAULT ")
                            .append(defaultValue);
                }
            }
        }
        return def.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ColumnInfo that = (ColumnInfo) o;
        return size == that.size && decimalDigits == that.decimalDigits && nullable == that.nullable && autoIncrement == that.autoIncrement && name.equals(that.name) && columnType.equals(that.columnType) && Objects.equals(defaultValue, that.defaultValue) && Objects.equals(comment, that.comment);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, columnType, size, decimalDigits, nullable, defaultValue, autoIncrement, comment);
    }

    private static String escapeSqlString(String value) {
        if (value == null) {
            return "";
        }
        return value.replace("'", "''");
    }

}